package com.ml.rabbitmq.producer;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;
/**
 * @author: 学相伴-飞哥
 * @description: OrderService
 * @Date : 2021/3/4
 */
//@Component
@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 1: 定义交换机
    private String exchangeFanoutName = "fanout_order_exchange";
    private String exchangeDirectName = "direct_order_exchange";
    private String exchangeTopicName = "topic_order_exchange";
    private String exchangeTTLName = "ttl_direct_exchange";
    // 2: 路由key
    private String routeKey = "";

    public void makeOrder(Long userId, Long productId, int num) {
        // 1： 模拟用户下单
        String orderNumer = UUID.randomUUID().toString();
        // 2: 根据商品id productId 去查询商品的库存
        // int numstore = productSerivce.getProductNum(productId);
        // 3:判断库存是否充足
        // if(num >  numstore ){ return  "商品库存不足..."; }
        // 4: 下单逻辑
        // orderService.saveOrder(order);
        // 5: 下单成功要扣减库存
        // 6: 下单完成以后
        System.out.println("用户 " + userId + ",订单编号是：" + orderNumer);
        // 发送订单信息给RabbitMQ fanout
        rabbitTemplate.convertAndSend(exchangeFanoutName, routeKey, orderNumer);
    }

    public void makeDirectOrder(Long userId, Long productId, int num) {
        // 1： 模拟用户下单
        String orderNumer = UUID.randomUUID().toString();
        // 2: 下单完成以后
        System.out.println("用户 " + userId + ",订单编号是：" + orderNumer);
        // 发送订单信息给RabbitMQ direct
        rabbitTemplate.convertAndSend(exchangeDirectName, "email", orderNumer);
        rabbitTemplate.convertAndSend(exchangeDirectName, "weixin", orderNumer);
    }

    public void makeTopicOrder(Long userId, Long productId, int num) {
        // 1： 模拟用户下单
        String orderNumer = UUID.randomUUID().toString();
        // 2: 下单完成以后
        System.out.println("用户 " + userId + ",订单编号是：" + orderNumer);
        String routingKey = "com.email.weixin.send.test";
        // *.email.#   email
        // com.#       sms
        // #.weixin.#  weixin
        // 发送订单信息给RabbitMQ topic
        rabbitTemplate.convertAndSend(exchangeTopicName, routingKey, orderNumer);
    }

    public void makeTTL(Long userId, Long productId, int num) {
        // 1： 模拟用户下单
        String orderNumer = UUID.randomUUID().toString();
        // 2: 下单完成以后
        System.out.println("用户 " + userId + ",订单编号是：" + orderNumer);
        String routingKey = "ttl";
        // 发送订单信息给RabbitMQ
        rabbitTemplate.convertAndSend(exchangeTTLName, routingKey, orderNumer);
    }

    public void makeTTLMessage(Long userId, Long productId, int num) {
        // 1： 模拟用户下单
        String orderNumer = UUID.randomUUID().toString();
        // 2: 下单完成以后
        System.out.println("用户 " + userId + ",订单编号是：" + orderNumer);
        String routingKey = "ttl-message";
        //给消息设置过期时间
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");//ms
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };

        // 发送订单信息给RabbitMQ
        rabbitTemplate.convertAndSend(exchangeTTLName, routingKey, orderNumer, messagePostProcessor);
    }
}
